#include <iostream>
#include <FL/Fl.h>
#include <Fl/Fl_Gl_Window.h>
#include <FL/gl.h>
#include <Gl/Glu.h>

class MyWindow : public Fl_Gl_Window
{
    void InitializeGL();
    virtual void draw();
    void DrawCube();

public:
    MyWindow::MyWindow(int width, int height, const char *title) : Fl_Gl_Window(width, height, title)
    {
        mode(FL_RGB | FL_ALPHA | FL_DEPTH | FL_DOUBLE);
    }
};
void MyWindow::InitializeGL()
{
    glClearColor(.1f, .1f, .1f, 1);
    glEnable(GL_DEPTH_TEST);
}
void MyWindow::DrawCube()
{
    glBegin(GL_QUADS);
    // front
    glColor3f(1, 0, 0);
    glVertex3f(-1, 1, 1);
    glVertex3f(-1, -1, 1);
    glVertex3f(1, -1, 1);
    glVertex3f(1, 1, 1);

    // back
    glColor3f(0, 1, 0);
    glVertex3f(-1, 1, -1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, -1, -1);
    glVertex3f(-1, -1, -1);

    // top
    glColor3f(0, 0, 1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, 1, 1);
    glVertex3f(1, 1, 1);
    glVertex3f(1, 1, -1);

    // bottom
    glColor3f(1, 1, 0);
    glVertex3f(-1, -1, -1);
    glVertex3f(1, -1, -1);
    glVertex3f(1, -1, 1);
    glVertex3f(-1, -1, 1);

    // left
    glColor3f(0, 1, 1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, 1, 1);

    // right
    glColor3f(1, 0, 1);
    glVertex3f(1, 1, 1);
    glVertex3f(1, -1, 1);
    glVertex3f(1, -1, -1);
    glVertex3f(1, 1, -1);
    glEnd();
}
void MyWindow::draw()
{
    static bool firstTime = true;
    if (firstTime)
    {
        InitializeGL();
        firstTime = false;
    } // if

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the color and depth buffer

    // view transformations
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1, 1, -1, 1, 1, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);

    // draw something
    DrawCube();
}

int main(int argc, char **argv)
{
    MyWindow *window = new MyWindow(340, 180, "你好，中国！");
    window->end();
    window->show(argc, argv);
    return Fl::run();
}
